% X = SERIALIZE_IMAGE(IMAGE_NAME)  Load an image and serialize the image
%                                  data
%
%  Input
%         image_name Pathname of the image to be loaded
%
%  Output
%         x   Serialized image, with the following interleaving:
%              [r(1) g(1) b(1) r(2) g(2) b(2) ... r(n) g(n) b(n)]

function x = serialize_image(image_name)
    
    global trans_info

    % Load the image using imread
    img = imread(image_name);
    
    % Separate out the RGB channels
    r_channel = img(:,:,1);
    g_channel = img(:,:,2);
    b_channel = img(:,:,3);
    
    % Interleave into a serial stream
    [r c] = size(r_channel);
    trans_info.imageHeight = r; %Store image dimensions
    trans_info.imageWidth = c;
    
    %Init array
    x = zeros(1, 3 * r * c);

    k = 3; %Index into output array
    for i = 1 : r
        for j = 1 : c
            x(k) =   r_channel(i,j);
            x(k+1) = g_channel(i,j);
            x(k+2) = b_channel(i,j);
            k = k + 3;
        end
    end
end